﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import Engine.Events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;
    import Engine.*;

    public class ScaffoldEffect extends MapResourceEffect {

        private const TARGET_BUILDING_NAME:String = "targetBuilding";

        private var m_currentStage:int;
        private var m_maxStage:int;
        private var m_startStage:int;
        private var m_endStage:int;
        private var m_targetBuildingName:String;
        private var m_isLoading:Boolean = false;
        private var m_isItemImagesLoaded:Boolean = false;
        private var m_itemImageLoadedHash:Object;
        private var m_bitmapDataHash:Object;
        private var m_isAnimationCompleted:Boolean = false;
        private var m_currentNumFloors:int;
        private var m_maxNumFloors:int;
        private var m_backBitmap:Bitmap;
        private var m_frontBitmap:Bitmap;
        private var m_lastRenderedNumFloors:int = -1;
        private var m_targetBuildingOffsetX:int;
        private var m_targetBuildingOffsetY:int;
        private var m_targetBuildingDirection:int;
        private var m_originalContentBitmapData:BitmapData;
        private var m_originalContentPosition:Point;
        private var m_animationCompleteCallback:Function = null;

        public function ScaffoldEffect(_arg1:MapResource, _arg2:int=1, _arg3:int=-1, _arg4:int=3, _arg5:String=""){
            super(_arg1, 1);
            this.m_maxStage = _arg4;
            this.m_startStage = _arg2;
            this.m_endStage = _arg3;
            this.m_targetBuildingName = _arg5;
            this.validateProperties();
            this.m_currentStage = this.m_startStage;
            this.animate(1);
        }
        public function get startStage():int{
            return (this.m_startStage);
        }
        public function set startStage(_arg1:int):void{
            this.m_startStage = _arg1;
        }
        public function get endStage():int{
            return (this.m_endStage);
        }
        public function set endStage(_arg1:int):void{
            this.m_endStage = _arg1;
        }
        public function get maxStage():int{
            return (this.m_maxStage);
        }
        public function set maxStage(_arg1:int):void{
            this.m_maxStage = _arg1;
        }
        public function get targetBuildingName():String{
            return (this.m_targetBuildingName);
        }
        public function set targetBuildingName(_arg1:String):void{
            this.m_targetBuildingName = _arg1;
        }
        public function get animationCompleteCallback():Function{
            return (this.m_animationCompleteCallback);
        }
        public function set animationCompleteCallback(_arg1:Function):void{
            this.m_animationCompleteCallback = _arg1;
        }
        override public function animate(_arg1:uint):Boolean{
            if (this.m_isAnimationCompleted){
                return (false);
            };
            if (((((isMapResourceLoaded) && (!(this.m_isLoading)))) && (!(this.m_isItemImagesLoaded)))){
                this.loadAnimationEffect();
                if (this.startStage != this.endStage){
                    return (true);
                };
                this.m_isAnimationCompleted = true;
                if (this.m_animationCompleteCallback != null){
                    this.m_animationCompleteCallback();
                };
                return (false);
            };
            if (((this.m_isLoading) && (!(this.m_isItemImagesLoaded)))){
                return (true);
            };
            if (this.m_isItemImagesLoaded){
                this.m_currentStage = (this.m_currentStage + 1);
                if (this.m_currentStage > this.m_endStage){
                    this.m_currentStage = this.m_endStage;
                    this.m_isAnimationCompleted = true;
                    if (this.m_animationCompleteCallback != null){
                        this.m_animationCompleteCallback();
                        this.m_currentStage = (this.m_maxStage + 1);
                    };
                };
                if (this.m_currentStage > this.m_maxStage){
                    this.cleanUpBitmaps();
                    return (false);
                };
                this.reattach();
            };
            return (true);
        }
        override public function cleanUp():void{
            this.m_itemImageLoadedHash = null;
            this.m_bitmapDataHash = null;
            this.cleanUpBitmaps();
            this.m_isAnimationCompleted = true;
            m_isCompleted = true;
        }
        override public function reattach():void{
            var _local6:Vector3;
            var _local7:IsoRect;
            var _local8:Number;
            var _local9:Number;
            var _local10:Number;
            var _local11:Number;
            var _local12:Number;
            var _local13:Number;
            var _local14:Number;
            var _local15:Number;
            var _local16:Number;
            var _local17:Number;
            var _local18:Number;
            var _local19:Number;
            var _local20:BitmapData;
            var _local21:BitmapData;
            var _local22:Point;
            var _local23:Number;
            var _local24:Number;
            var _local25:Point;
            var _local26:BitmapData;
            var _local27:Number;
            var _local28:Number;
            var _local29:String;
            var _local30:int;
            var _local31:int;
            var _local32:Number;
            var _local33:String;
            var _local34:int;
            var _local35:Sprite;
            var _local36:int;
            var _local37:BitmapData;
            var _local38:Bitmap;
            var _local39:BitmapData;
            if (!this.m_isItemImagesLoaded){
                return;
            };
            if (((this.m_targetBuildingName) && (!((this.m_targetBuildingDirection == m_mapResource.getDirection()))))){
                this.loadAnimationEffect();
                return;
            };
            this.validateProperties();
            var _local1:Number = 4;
            if (this.m_currentStage <= this.m_maxNumFloors){
                this.m_currentNumFloors = this.m_currentStage;
            } else {
                this.m_currentNumFloors = 1;
            };
            if (this.m_currentStage > this.m_maxStage){
                this.m_currentNumFloors = 0;
                this.cleanUpBitmaps();
            };
            if ((((this.m_currentNumFloors > 0)) && (!((this.m_currentNumFloors == this.m_lastRenderedNumFloors))))){
                this.cleanUpBitmaps();
                _local6 = m_mapResource.getReference().getSize();
                _local7 = IsoRect.getIsoRectFromSize(_local6);
                _local8 = 16;
                _local9 = 20;
                _local10 = (_local8 >> 1);
                _local11 = (_local9 >> 1);
                _local12 = this.m_bitmapDataHash["1SW"].width;
                _local13 = this.m_bitmapDataHash["1SW"].height;
                _local14 = (_local12 - _local8);
                _local15 = (_local13 - _local9);
                _local16 = 0;
                _local17 = (((_local7.height - _local7.bottom.y) + _local15) - (_local9 * -((this.m_currentNumFloors - 2))));
                _local18 = ((_local7.width + _local10) - _local1);
                _local19 = (((_local7.height + (_local9 * this.m_currentNumFloors)) + (_local15 >> 1)) - _local1);
                _local20 = new BitmapData(_local18, _local19, true, 0);
                _local21 = _local20.clone();
                _local22 = new Point();
                _local23 = (_local6.x * IsoRect.TILE_SIZE);
                _local24 = (-(_local6.y) * IsoRect.TILE_SIZE);
                _local25 = new Point();
                _local27 = 0;
                _local28 = 0;
                _local30 = 0;
                while (_local30 < this.m_currentNumFloors) {
                    _local32 = -(_local30);
                    _local33 = (_local32) ? "2" : "1";
                    _local29 = (_local33 + "SW");
                    _local26 = this.m_bitmapDataHash[_local29];
                    _local27 = -(_local10);
                    _local28 = ((-(_local15) + _local11) - _local1);
                    _local31 = _local6.x;
                    if (_local31 > 0){
                        while (_local31--) {
                            _local25 = IsoRect.tilePosToPixelPos((_local31 * IsoRect.TILE_SIZE), _local24, _local32);
                            _local22.x = ((_local25.x + _local16) + _local27);
                            _local22.y = ((_local25.y + _local17) + _local28);
                            _local21.copyPixels(_local26, _local26.rect, _local22, null, null, true);
                        };
                    };
                    _local29 = (_local33 + "NW");
                    _local26 = this.m_bitmapDataHash[_local29];
                    _local27 = 0;
                    _local28 = 0;
                    _local31 = _local6.y;
                    if (_local31 > 0){
                        while (_local31--) {
                            _local25 = IsoRect.tilePosToPixelPos(0, (-(((_local6.y - 1) - _local31)) * IsoRect.TILE_SIZE), _local32);
                            _local22.x = ((_local25.x + _local16) + _local27);
                            _local22.y = ((_local25.y + _local17) + _local28);
                            _local21.copyPixels(_local26, _local26.rect, _local22, null, null, true);
                        };
                    };
                    _local29 = (_local33 + "NE");
                    _local26 = this.m_bitmapDataHash[_local29];
                    _local27 = 0;
                    _local28 = (-(_local15) + _local11);
                    _local31 = _local6.x;
                    if (_local31 > 0){
                        while (_local31--) {
                            _local25 = IsoRect.tilePosToPixelPos((_local31 * IsoRect.TILE_SIZE), 0, _local32);
                            _local22.x = ((_local25.x + _local16) + _local27);
                            _local22.y = ((_local25.y + _local17) + _local28);
                            _local20.copyPixels(_local26, _local26.rect, _local22, null, null, true);
                        };
                    };
                    _local29 = (_local33 + "SE");
                    _local26 = this.m_bitmapDataHash[_local29];
                    _local27 = -(_local10);
                    _local28 = _local1;
                    _local31 = _local6.y;
                    if (_local31 > 0){
                        while (_local31--) {
                            _local25 = IsoRect.tilePosToPixelPos(_local23, (-(((_local6.y - 1) - _local31)) * IsoRect.TILE_SIZE), _local32);
                            _local22.x = ((_local25.x + _local16) + _local27);
                            _local22.y = ((_local25.y + _local17) + _local28);
                            _local20.copyPixels(_local26, _local26.rect, _local22, null, null, true);
                        };
                    };
                    _local30++;
                };
                this.m_frontBitmap = new Bitmap(_local21);
                this.m_frontBitmap.smoothing = true;
                this.m_frontBitmap.pixelSnapping = PixelSnapping.AUTO;
                this.m_backBitmap = new Bitmap(_local20);
                this.m_backBitmap.smoothing = true;
                this.m_backBitmap.pixelSnapping = PixelSnapping.AUTO;
                this.m_lastRenderedNumFloors = this.m_currentNumFloors;
            };
            var _local2:Number = (-(m_mapResource.displayObjectOffsetX) / m_mapResource.displayObject.scaleX);
            var _local3:Number = (-(m_mapResource.displayObjectOffsetY) / m_mapResource.displayObject.scaleY);
            var _local4:Vector2 = IsoMath.getPixelDeltaFromTileDelta(0, m_mapResource.getSize().y);
            var _local5:int = (_local4.x / m_mapResource.displayObject.scaleX);
            if (this.m_currentNumFloors > 0){
                _local34 = 2;
                this.m_frontBitmap.x = (this.m_backBitmap.x = ((_local2 + _local5) - _local34));
                this.m_frontBitmap.y = (this.m_backBitmap.y = ((_local3 - this.m_backBitmap.height) + _local34));
                _local35 = (m_mapResource.getDisplayObject() as Sprite);
                _local35.addChild(this.m_frontBitmap);
                _local36 = (_local35.getChildIndex(m_mapResource.content) - 1);
                if (_local36 < 0){
                    _local36 = 0;
                };
                _local35.addChildAt(this.m_backBitmap, _local36);
            };
            if (((this.m_targetBuildingName) && ((this.m_currentStage >= this.m_maxStage)))){
                _local37 = this.m_bitmapDataHash[this.TARGET_BUILDING_NAME];
                if ((m_mapResource.content is Bitmap)){
                    _local38 = (m_mapResource.content as Bitmap);
                    _local39 = _local38.bitmapData;
                    if (_local39 != _local37){
                        this.m_originalContentBitmapData = _local39;
                        this.m_originalContentPosition = new Point(_local38.x, _local38.y);
                    };
                };
                if (((_local37) && (_local38))){
                    _local38.bitmapData = _local37;
                    _local38.smoothing = true;
                    _local38.pixelSnapping = PixelSnapping.AUTO;
                    _local38.x = ((_local2 + _local5) + this.m_targetBuildingOffsetX);
                    _local38.y = ((_local3 - _local38.height) + this.m_targetBuildingOffsetY);
                };
            };
        }
        protected function validateProperties():void{
            if (this.m_maxStage < 2){
                this.m_maxStage = 2;
            };
            if (this.m_endStage < this.m_startStage){
                this.m_endStage = this.m_startStage;
            };
            this.m_maxNumFloors = Math.max(1, (this.m_maxStage - 1));
            if (this.m_currentStage < this.m_startStage){
                this.m_currentStage = this.m_startStage;
            } else {
                if (this.m_currentStage > this.m_endStage){
                    this.m_currentStage = this.m_endStage;
                };
            };
        }
        protected function cleanUpBitmaps():void{
            if (this.m_frontBitmap){
                if (this.m_frontBitmap.parent){
                    this.m_frontBitmap.parent.removeChild(this.m_frontBitmap);
                };
                this.m_frontBitmap.bitmapData.dispose();
                this.m_frontBitmap = null;
            };
            if (this.m_backBitmap){
                if (this.m_backBitmap.parent){
                    this.m_backBitmap.parent.removeChild(this.m_backBitmap);
                };
                this.m_backBitmap.bitmapData.dispose();
                this.m_backBitmap = null;
            };
            this.m_lastRenderedNumFloors = -1;
        }
        protected function loadAnimationEffect():void{
            var imageXML:* = null;
            var imageName:* = null;
            var itemImage:* = null;
            var imageDirection:* = null;
            var targetBuildingXML:* = null;
            var targetBuildingImagesXMList:* = null;
            var targetBuildingImageXMLList:* = null;
            var entry:* = null;
            var buildingChildXML:* = null;
            var tempXML:* = null;
            this.m_isItemImagesLoaded = false;
            this.m_itemImageLoadedHash = {};
            this.m_bitmapDataHash = {};
            var imageXMLList:* = Global.gameSettings.getScaffoldSetByName("scaffoldA");
            for each (imageXML in imageXMLList) {
                imageName = imageXML.@name;
                this.m_itemImageLoadedHash[imageName] = false;
            };
            if (this.m_targetBuildingName){
                this.m_targetBuildingDirection = m_mapResource.getDirection();
                imageDirection = ItemImage.getDirectionCode(this.m_targetBuildingDirection);
                targetBuildingXML = Global.gameSettings.getItemByName(this.m_targetBuildingName).xml;
                targetBuildingImagesXMList = targetBuildingXML.image.(@name == "planted");
                targetBuildingImageXMLList = targetBuildingXML.image.(((@name == "planted")) && ((@direction == imageDirection)));
                if (((!(targetBuildingImageXMLList)) || (!(targetBuildingImageXMLList.length())))){
                    targetBuildingImageXMLList = targetBuildingXML.image.(((@name == "planted")) && ((@direction == "ALL")));
                };
                if (((!(targetBuildingImageXMLList)) || (!(targetBuildingImageXMLList.length())))){
                    targetBuildingImageXMLList = targetBuildingXML.image;
                };
                imageXML = targetBuildingImageXMLList[0];
                for each (entry in targetBuildingImageXMLList) {
                    if (String(entry.@direction) == imageDirection){
                        imageXML = entry;
                    };
                };
                imageXML = imageXML.copy();
                imageXML.@name = this.TARGET_BUILDING_NAME;
                this.m_itemImageLoadedHash[this.TARGET_BUILDING_NAME] = false;
                itemImage = new ItemImage(imageXML);
                itemImage.name = this.TARGET_BUILDING_NAME;
                itemImage.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                itemImage.load();
                buildingChildXML = null;
                for each (tempXML in imageXML.children()) {
                    if (tempXML.@type == "building"){
                        buildingChildXML = tempXML;
                        break;
                    };
                };
                this.m_targetBuildingOffsetX = ((buildingChildXML == null)) ? 0 : int(buildingChildXML.@offsetX);
                this.m_targetBuildingOffsetY = ((buildingChildXML == null)) ? 0 : int(buildingChildXML.@offsetY);
            };
            for each (imageXML in imageXMLList) {
                itemImage = new ItemImage(imageXML);
                itemImage.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                itemImage.load();
            };
            this.m_isLoading = true;
        }
        protected function onItemImageLoaded(_arg1:LoaderEvent):void{
            var _local2:ItemImage;
            var _local3:ItemImageInstance;
            var _local4:DisplayObject;
            var _local5:Bitmap;
            var _local6:String;
            var _local7:Boolean;
            var _local8:Boolean;
            if (!this.m_isItemImagesLoaded){
                _local2 = (_arg1.target as ItemImage);
                _local2.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                if (m_isCompleted){
                    return;
                };
                _local3 = _local2.getInstance();
                if (!_local3){
                    return;
                };
                _local4 = (_local2.getInstance().image as DisplayObject);
                if ((_local4 is Bitmap)){
                    _local5 = (_local4 as Bitmap);
                } else {
                    if ((_local4 is CompositeItemImage)){
                        _local5 = ((_local4 as CompositeItemImage).getBuildingImageByType(ItemImageInstance.BUILDING_IMAGE) as Bitmap);
                    };
                };
                this.m_bitmapDataHash[_local2.name] = _local5.bitmapData;
                _local6 = _local2.name;
                this.m_itemImageLoadedHash[_local6] = true;
                _local7 = true;
                for each (_local8 in this.m_itemImageLoadedHash) {
                    if (!_local8){
                        _local7 = false;
                        break;
                    };
                };
                if (_local7){
                    this.m_isLoading = false;
                    this.m_isItemImagesLoaded = true;
                    this.reattach();
                };
            };
        }

    }
}//package Classes.effects 
